
from _application import *
from _text import *
from _canvas import *

import time

app = Application()

def available_fonts():
     """Retrieves a list (Unicode) of all fonts available in the device."""
     return []


def _text_query(label, qtype, initial_value):
     global app
     dlg = gtk.Dialog(None, app._getWindow(), gtk.DIALOG_MODAL, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
     label_ctrl = gtk.Label(label)
     label_ctrl.show()
     dlg.vbox.pack_start(label_ctrl, False, False, 0)
     entry = gtk.Entry()
	 
     if qtype == 'code':
         entry.set_invisible_char('*')
         entry.set_visibility(False)
	 
     if initial_value is not None and \
	    qtype != 'code':
         entry.set_text(initial_value)
	 
     entry.show()
     dlg.vbox.pack_start(entry, False, False, 0)
     
     resp = dlg.run()
     dlg.hide()

     if resp == gtk.RESPONSE_ACCEPT:
         res = entry.get_text()
     else:
         res = None
	 
     return res
     
def _num_query_changed(entry, qtype):
    txt = entry.get_chars(0, -1)
#    if qtype == 'number':
         #char set: 1234567890+-.E , valid 1234567890.
#        for i in txt:
#	    pass
#    elif qtype == 'float':
#        for i in txt
#	    pass
     
def _num_query(label, qtype, initial_value):
     global app
     dlg = gtk.Dialog(None, app._getWindow(), gtk.DIALOG_MODAL, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
     label_ctrl = gtk.Label(label)
     label_ctrl.show()
     dlg.vbox.pack_start(label_ctrl, False, False, 0)
     entry = gtk.Entry()
     entry.connect("changed", lambda entry: _num_query_changed(entry, qtype))
     if initial_value is not None:
         entry.set_text(str(initial_value))
	 
     entry.show()
     dlg.vbox.pack_start(entry, False, False, 0)
     
     resp = dlg.run()
     dlg.hide()

     if resp == gtk.RESPONSE_ACCEPT:
         if qtype == 'number':
             res = int(entry.get_text())
         else:
             res = float(entry.get_text())
     else:
         res = None
	 
     return res
     
def _date_query(label, qtype, initial_value):
     global app
     dlg = gtk.Dialog(None, app._getWindow(), gtk.DIALOG_MODAL, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
     label_ctrl = gtk.Label(label)
     label_ctrl.show()
     dlg.vbox.pack_start(label_ctrl, False, False, 0)
     
     if initial_value is None:
         initial_value = 0
     
     tm = time.localtime(initial_value)
     ed = gtk.Entry()
     ed.set_width_chars(2)
     ed.set_text(str(tm.tm_mday))
     ed.show()
     
     em = gtk.Entry()
     em.set_width_chars(2)
     em.set_text(str(tm.tm_mon))
     em.show()
     
     ey = gtk.Entry()
     ey.set_width_chars(4)
     ey.set_text(str(tm.tm_year))
     ey.show()
     
     sep1 = gtk.Label("/")
     sep1.show()
     sep2 = gtk.Label("/")
     sep2.show()
     
     hb = gtk.HBox(False, 0)
     hb.pack_start(ed, False, False, 0)
     hb.pack_start(sep1, False, False, 0)
     hb.pack_start(em, False, False, 0)
     hb.pack_start(sep2, False, False, 0)
     hb.pack_start(ey, False, False, 0)
     hb.show()
     
     dlg.vbox.pack_start(hb, False, False, 0)
          
     resp = dlg.run()
     dlg.hide()

     if resp == gtk.RESPONSE_ACCEPT:
         strtm = ed.get_text()+"/"+em.get_text()+"/"+ey.get_text()
         tm = time.strptime(strtm, "%d/%m/%Y")      
         res = time.mktime(tm)
     else:
         res = None
	 
     return res
     
def _time_query(label, qtype, initial_value):
     global app
     dlg = gtk.Dialog(None, app._getWindow(), gtk.DIALOG_MODAL, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
     label_ctrl = gtk.Label(label)
     label_ctrl.show()
     dlg.vbox.pack_start(label_ctrl, False, False, 0)
     
     if initial_value is None:
         initial_value = 0
     
     tm = time.localtime(initial_value)
     eh = gtk.Entry()
     eh.set_width_chars(2)
     eh.set_text(str(tm.tm_hour))
     eh.show()
     
     em = gtk.Entry()
     em.set_width_chars(2)
     em.set_text(str(tm.tm_min))
     em.show()
     
     sep = gtk.Label(":")
     sep.show()
     
     hb = gtk.HBox(False, 0)
     hb.pack_start(eh, False, False, 0)
     hb.pack_start(sep, False, False, 0)
     hb.pack_start(em, False, False, 0)
     hb.show()
     
     dlg.vbox.pack_start(hb, False, False, 0)
          
     resp = dlg.run()
     dlg.hide()

     if resp == gtk.RESPONSE_ACCEPT:
         strtm = "01/01/1970 "+eh.get_text()+":"+em.get_text()+":0"
         tm = time.strptime(strtm, "%d/%m/%Y %H:%M:%S")      
         res = time.mktime(tm)
     else:
         res = None
	 
     return res
     
def _query_query(label):
     global app
     dlg = gtk.MessageDialog(app._getWindow(), gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL, label)
     resp = dlg.run()
     dlg.hide()
     
     if resp == gtk.RESPONSE_OK:
         res = True
     else:
         res = None
	 
     return res


def query(label, qtype, initial_value = None):
     """Performs a query with a single-field dialog.
     
     The prompt is set to label, and the type of the
     dialog is defined by type. The value of type can be any of the following strings:
     o   'text'
     o   'code'
     o   'number'
     o   'date'
     o   'time'
     o   'query'
     o   'float'
     
     The type of the optional initial_value parameter and the returned input depend on the value of type:
     o For text fields, ('text', 'code') it is Unicode
     o For number fields, it is numeric
     o For date fields, it is seconds since epoch rounded down to the nearest local midnight
     
     A simple confirmation query and time query take no initial value and return True/None and
     seconds since local midnight, correspondingly. All queries return None if the users cancel the dialog.
     """
     
     if qtype in ('text', 'code'):
         res = _text_query(label, qtype, initial_value)
     elif qtype in ('number', 'float'):
         res = _num_query(label, qtype, initial_value)
     elif qtype == 'date':
         res = _date_query(label, qtype, initial_value)
     elif qtype == 'time':
         res = _time_query(label, qtype, initial_value)
     elif qtype == 'query':
         res = _query_query(label)
     else:
         print "ERROR: unknown query type - " + qtype
	 
     return res

def multi_query(label_1, label_2):
     """A two-field text (Unicode) input dialog. 
     
     Returns None if the users cancel the dialog.
     """
     global app
     dlg = gtk.Dialog(None, app._getWindow(), gtk.DIALOG_MODAL, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
     label_ctrl1 = gtk.Label(label_1)
     label_ctrl1.show()
     dlg.vbox.pack_start(label_ctrl1, False, False, 0)
     entry1 = gtk.Entry()
     entry1.show()
     dlg.vbox.pack_start(entry1, False, False, 0)
     
     label_ctrl2 = gtk.Label(label_2)
     label_ctrl2.show()
     dlg.vbox.pack_start(label_ctrl2, False, False, 0)
     entry2 = gtk.Entry()
     entry2.show()
     dlg.vbox.pack_start(entry2, False, False, 0)
     
     resp = dlg.run()
     dlg.hide()

     if resp == gtk.RESPONSE_ACCEPT:
         res = (entry1.get_text(), entry2.get_text())
     else:
         res = None
	 
     return res

def note(text, ntype = "info", nglobal = 0):
     """Displays a note dialog of the chosen type with text (Unicode). 
     
     type can be one of the following strings: 
     'error', 'info', or 'conf'.
     """
     if ntype == "error":
         icon = gtk.MESSAGE_ERROR
     elif ntype in ("info", "conf"):
         icon = gtk.MESSAGE_INFO
     else:
        #error
        pass

     global app
     dlg = gtk.MessageDialog(app._getWindow(), gtk.DIALOG_MODAL, icon, gtk.BUTTONS_OK, text)
     dlg.run()
     dlg.hide()

     
def popup_menu(list, label = None):
     """A pop-up menu style dialog. 
     
     list representing the menu contents can be a list of Unicode
     strings or a list of Unicode string pairs (tuples). The resulting dialog list is then a single-style or
     a double-style list. A single-style list is shown in full; whereas a double-style list shows the
     items one at a time. Returns None if the users cancel the operation.
     """
     index = 0
     return index

def selection_list(choices, search_field = 0):
     """Executes a dialog that allows the users to select a list item and returns the index of the chosen
     item, or None if the selection is cancelled by the users.
     
     (choices=list [, search_field=0]) consists of keywords and values, where
     choices is a list of Unicode strings.
     o search_field is 0 (disabled) by default and is optional. Setting it to 1 enables a search
     o field (find pane) that facilitates searching for items in long lists. If enabled, the search field
     appears after you press a letter key.
     """
     _data = []
     for item in choices:
        if isinstance(item, tuple):
	    item = ": ".join(item)
	_data.append(item)

#     index = wx.GetSingleChoiceIndex(u"", "Choice", _data)
#     if index >= 0:
#	return index
#     else:
#	return None
  
def multi_selection_list(choices, style = 'checkbox', search_field = 0):
     """Executes a dialog that allows the users to select multiple list items. 
     
     Returns a tuple of indexes (a pair of Unicode strings) of the chosen items, 
     or None if the selection is cancelled by the users.
     
     o choices is a list of Unicode strings.
     o style is an optional string; the default value being 'checkbox'.
     If 'checkbox' is given,
     the list will be a checkbox list, where empty checkboxes indicate what items can be marked.
     The other possible value that can be set for style is 'checkmark'. If 'checkmark' is
     given, the list will be a markable list, which lists items but does not indicate specifically that
     items can be selected. To select items on a markable list, use the Navigation key to browse
     the list and the Edit key to select an item. 
     o search_field is 0 (disabled) by default and is optional. Setting it to 1 enables a search
     field (find pane) that facilitates searching for items in long lists. If enabled, the search field is
     always visible with checkbox lists; with markable lists it appears by pressing a letter key.
     """
     indexes = (0)
     return indexes     
